import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'
import crypto from 'crypto'

// Custom APIs for renderer
const api = {
  // MD5 加密函数
  md5: (string) => {
    return crypto.createHash('md5').update(string).digest('hex')
  },
  // 获取屏幕源
  getDesktopSources: async (options) => {
    return await ipcRenderer.invoke('get-desktop-sources', options)
  }
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  window.electron = electronAPI
  window.api = api
}
